/**UserMenu.java
 * 
 * This class is for the whole User Menu.
 * 
 * @author Christopher Carithers
 * Last Updated: 4/15/2014
 *
 */

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Vector;

public class UserMenu 
{
   private String m_TitleOfMenu;
   private String m_OptionDoesNotExist;
   private List<MenuOption> m_MenuOptions = new Vector<MenuOption>();
   
   public UserMenu(String titleOfMenu, String optionDoesNotExist)
   {
	   m_TitleOfMenu = titleOfMenu;
	   m_OptionDoesNotExist = optionDoesNotExist;
   }
   
   public boolean executeSelectedMenuOption(int userOptionSelected) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException
   {
      int currentMenuOption = 0;
      boolean found = false;
      String className;
      Method method;

      if(userOptionSelected > 0 && userOptionSelected <= m_MenuOptions.size())
      {
         while(currentMenuOption != userOptionSelected && found == false)
         {
            currentMenuOption++;
         }
     	 className = m_MenuOptions.get(currentMenuOption-1).getFunctionName();
     	 method = getClass().getMethod(className);
         method.invoke(this);
         found = true;
      }
      else
      {
    	    System.out.println("User option does not exist choose another.");
    	    return false;
      }
      return true;
   }
   
   public void getMenuOptionsList()
   {   
	   String menuOption;
	   for(int i = 0; i < m_MenuOptions.size(); i++)
       {
		  menuOption = m_MenuOptions.get(i).getOptionText();
		  System.out.println(menuOption);
       }
   }
   
   public void setMenuOptions(MenuOption menuOption)
   {
	   m_MenuOptions.add(menuOption);
   }
   
   public void setTitleOfMenu(String titleOfMenu)
   {
	   m_TitleOfMenu = titleOfMenu;
   }
   
   public String getTitleOfMenu()
   {
	   return m_TitleOfMenu;
   }
   
   public String getOptionDoesNotExist()
   {
	   return m_OptionDoesNotExist;
   }
   
   public int startNewGame()
   {
	   return 1;
   }
   
   public int options()
   {
	   return 2;
   }
   
   public int exitGame()
   {
	   System.out.println("Exiting Game");
	   return 0;
   }
}
